package com.beidasoft.web.util;

import com.beidasoft.web.util.fileUtil.Ks3Util;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.client.j2se.MatrixToImageWriter;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;

/**
 * Created by Administrator on 2016/10/12.
 */
public class QrCodeGenerateUtil {

    @Resource
    private Ks3Util ks3Util;


    /**
     * 默认构造函数
     */
    public QrCodeGenerateUtil() {

    }

    /**
     * 创建二维码图片
     *
     * @param http 查询人员信息链接
     * @param name 文件名
     * @param path 文件保存路径
     * @return true 成功; false 失败
     * @throws Exception
     */
    public boolean createQrCode(String http, String name, String path) throws Exception {
        boolean flag = true;
        try {
            int width = 300;
            int height = 300;
            String format = "png";
            name = name + ".png";
            //定义二维码内容参数
            HashMap hints = new HashMap();
            //设置字符集编码格式
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            //设置容错等级，在这里我们使用M级别
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
            //设置边框距
            hints.put(EncodeHintType.MARGIN, 2);
            //生成二维码
            //指定二维码内容
            BitMatrix bitMatrix = new MultiFormatWriter().encode(http, BarcodeFormat.QR_CODE, width, height, hints);

            //将生成的二维码临时保存在当前项目所在地址
            String property = System.getProperty("user.dir");
            File file = new File(property + File.separator + name);
            //生成二维码
            MatrixToImageWriter.writeToFile(bitMatrix, format, file);

            InputStream inputStream = new FileInputStream(file);


            //将临时文件上传到文件服务器
            ks3Util.uploadFile(path + name,inputStream);
            inputStream.close();
            //删除临时文件
            file.delete();


            //指定生成图片的保存路径
            //File file = new File(path + name);
            //生成二维码
            //MatrixToImageWriter.writeToFile(bitMatrix, format, file);

        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }


    public boolean createQrCode11111(String http, String name, String path) throws Exception {
        boolean flag = true;
        try {
            int width = 300;
            int height = 300;
            String format = "png";
            name = name + ".png";
            //定义二维码内容参数
            HashMap hints = new HashMap();
            //设置字符集编码格式
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            //设置容错等级，在这里我们使用M级别
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
            //设置边框距
            hints.put(EncodeHintType.MARGIN, 2);
            //生成二维码
            //指定二维码内容
            BitMatrix bitMatrix = new MultiFormatWriter().encode(http, BarcodeFormat.QR_CODE, width, height, hints);

            //将生成的二维码临时保存在当前项目所在地址
            //String property = System.getProperty("user.dir");
            String property = "/data/fzb_three/temp/tempDirQg";
            File file = new File(property + "/" + name);
            //生成二维码
            MatrixToImageWriter.writeToFile(bitMatrix, format, file);

            InputStream inputStream = new FileInputStream(file);


            inputStream.close();
            //删除临时文件



            //指定生成图片的保存路径
            //File file = new File(path + name);
            //生成二维码
            //MatrixToImageWriter.writeToFile(bitMatrix, format, file);

        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
}
